package com.Renderer;

import javax.microedition.khronos.opengles.GL10;
import android.content.Context;
import android.view.MotionEvent;
import com.Core.GameEngine;
import com.DataWrapper.EventTracker;
import com.DataWrapper.MapModel;
import com.Object.ObjType;
import com.Object.SeaObject;

public class GameField
{	private MapModel field;
	private EventTracker tracker;
	
	public GameField()
	{	field = GameEngine.mapModel;
		tracker = GameEngine.tracker;
		GameEngine.texLoader = new ObjectTextureLoader();
	}
	public static boolean isInside(float x, float y)
	{	if (GameEngine.display.getWidth()<GameEngine.display.getHeight())
			return y<=GameEngine.pjRight;
		else
			return x<=GameEngine.pjTop;
	}
	public void loadTexture(GL10 gl, Context context)
	{	GameEngine.texLoader.loadTexture(gl, context);
	}
	public void draw(GL10 gl)
	{	for (int i=0;i<GameEngine.MAP_SIZE;i++)
			for (int j=0;j<GameEngine.MAP_SIZE;j++)
				field.Get(i,j).DrawAtCell(gl,i,j);
	}
	public void drawOverlay(GL10 gl)
	{	switch (GameEngine.tracker.action)
		{	case MotionEvent.ACTION_DOWN:
			case MotionEvent.ACTION_MOVE:
				if (isInside(GameEngine.tracker.x, GameEngine.tracker.y))
					Overlay(gl);
				break;
			case MotionEvent.ACTION_UP:
				break;
		}
		int wd=GameEngine.display.getWidth(),ht=GameEngine.display.getHeight();
		if (wd>ht)
			GameEngine.curObj.DrawAtPos(gl, GameEngine.UP_BOUND*wd/ht-1.0f, 0.5f, 2.0f);
		else
			GameEngine.curObj.DrawAtPos(gl, GameEngine.UP_BOUND-1.5f, GameEngine.UP_BOUND*ht/wd-1.0f, 2.0f);
	}
	private void Overlay(GL10 gl)
	{	if (tracker.isPosAvail())
		{	SeaObject obj = field.Get(tracker.curX, tracker.curY);
			if (obj.getType()==ObjType.Null)
				GameEngine.curObj.DrawAtCell(gl, tracker.curX, tracker.curY);
			else
				GameEngine.curObj.DrawAtPos(gl, tracker.x, tracker.y, 1.0f);
		}
		else
			GameEngine.curObj.DrawAtPos(gl, tracker.x, tracker.y, 1.0f);
	}
}
